﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using Entities_Layer;
using DAL.Repositories;
using System.Collections;
using System.Transactions;

namespace DAL.Repositories
{
    public class SateliteRepository : IRepository<Satelite>
    {
        private List<IEntity> _insertItems;
        private List<IEntity> _updateItems;
        private List<IEntity> _deleteItems;

        public SateliteRepository() 
        {
            _insertItems = new List<IEntity>();
            _updateItems = new List<IEntity>();
            _deleteItems = new List<IEntity>();
        }

        public void Insert(Satelite entity)
        {
            _insertItems.Add(entity);
        }

        public void Delete(Satelite entity)
        {
            _updateItems.Add(entity);
        }

        public void Update(Satelite entity)
        {
            _deleteItems.Add(entity);
        }


        public IEnumerable<Satelite> GetAll()
        {
            throw new NotImplementedException();
        }

        public Satelite GetById(int id)
        {
            throw new NotImplementedException();
        }

        public void Save()
        {

            using (TransactionScope scope = new TransactionScope())
            {
                try
                {
                    if (_insertItems.Count > 0)
                    {
                        foreach (Satelite objSatelite in _insertItems)
                        {
                            InsertSatelite(objSatelite);
                        }
                    }

                    if (_updateItems.Count > 0)
                    {

                    }

                    if (_deleteItems.Count > 0)
                    {

                    }

                    scope.Complete();
                }

                catch (ApplicationException ex)
                {
                    Console.WriteLine("error" + ex);
                }

                finally
                {
                    Clear(); //--> este clear llama al metodo que vuele a los list a la normalidad
                }
            }
        }

        public void Clear()
        {
            _insertItems.Clear();
            _deleteItems.Clear();
            _updateItems.Clear();
        }

        private void InsertSatelite(Satelite objSatelite)
        {
            try
            {
                SqlCommand sql = new SqlCommand();
                //revisar si los nombres de los parametros son los del procedure o cualquiera sin importar
                sql.Parameters.Add(new SqlParameter("@idPlaneta", objSatelite.IdPlaneta));
                sql.Parameters.Add(new SqlParameter("@distanciaMediaOrbita", objSatelite.DistanciaMediaOrbita));
                sql.Parameters.Add(new SqlParameter("@caracteristicas", objSatelite.Caracteristicas));
    
                DataSet ds = DBAccess.ExecuteSPWithDS(ref sql, "pa_agregar_satelite");
            }
            catch (Exception x)
            {
                Console.WriteLine("error inesperado" + x);
            }
        }
    }
}
